package com.haochang.chunkvod.cloudpos;

import java.util.UUID;

import com.basewin.aidl.OnPrinterListener;
import com.haochang.base.BaseApplication;
import com.haochang.chunkvod.app.database.cash.PrintInfoDao;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.cash.PrintInfo;
import com.haochang.chunkvod.tools.vod.log.Logger;

import android.text.TextUtils;

/**
 * 第三方打印监听
 */
public class PrinterListener implements OnPrinterListener
{
	private PrintInfo printInfo = null;

	public PrinterListener(PrintInfo printInfo)
	{
		this.printInfo = printInfo;
	}

	@Override
	public void onError(int i, String s)
	{
		if (printInfo != null)
		{
			Logger.e("打印出错保存数据库");
			Logger.e(printInfo.getType() + ":" + printInfo.getId());
			if (TextUtils.isEmpty(printInfo.getId()))
				printInfo.setId(UUID.randomUUID().toString());
			PrintInfoDao.getDao().insert(PrintInfo.class, printInfo);
		}
		if (BaseApplication.appContext != null)
			ToastUtils.showText(BaseApplication.appContext, s);
		Logger.e("打印出错code:" + i + "msg:" + s);
	}

	@Override
	public void onFinish()
	{
		Logger.e("打印结束" + printInfo.getType() + "ID:" + printInfo.getId());
	}

	@Override
	public void onStart()
	{
		Logger.e("打印开始" + printInfo.getType() + "ID:" + printInfo.getId());
		//如果id不为空，则从数据库查询出来的，打印时需清空当前这条数据
		if (!TextUtils.isEmpty(printInfo.getId()))
		{
			PrintInfoDao.getDao().delete(PrintInfo.class, printInfo);
		}
	}
}
